package ldkxingzhe.l365calendar.utils;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import ldkxingzhe.l365calendar.App;

/**
 * 系统级的工具
 * Created by ke on 16-1-14.
 */
@SuppressWarnings("unused")
public class OSUtil {

    private OSUtil(){};

    public static float dp2px(int dp){
        return dp * getDensity(App.applicationContext);
    }

    private static float getDensity(Context context){
        return getDisplayMetrics(context).density;
    }

    @NonNull
    private static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

    /**
     * 获取屏幕宽度
     */
    public static int getScreenWidth(){
        return getDisplayMetrics(App.applicationContext).widthPixels;
    }

    /**
     * 获取应用的根目录
     */
    public static String getApplicationRootPath(String directory){
        if(directory == null){
            return Environment.getExternalStorageDirectory().getAbsolutePath() + "/L365Calendar";
        }else{
            return Environment.getExternalStorageDirectory().getAbsolutePath() + "/L365Calendar/" + directory;
        }
    }

    public static String getApplicationFilePath(@NonNull String directory,
                                                String fileName, String format){
        return getApplicationRootPath(directory) + "/" + fileName + "." + format;
    }


    public static int getTextHeight(Paint paint, String text){
        if(paint == null) return 0;
        Rect rect = new Rect();
        paint.getTextBounds(text,0, 2, rect);
        return rect.height();
    }
}
